﻿/// <reference path="_references.js" />

$(document).ready(function () {
    vm = null;
    if (vm == null) {
        loadPage(1, 10, '');
    }
    $(document).delegate("#FirstPageButton", "click", FirstPage);
    $(document).delegate("#LastPageButton", "click", LastPage);
    $(document).delegate("#prevPageButton", "click", previousPage);
    $(document).delegate("#nextPageButton", "click", nextPage);
    $(document).delegate("#rowsPerPage", "change", pageSizeChanged);
    $(document).delegate(".pageNumber", "click", pagedRows);

    $("#btnSearch").click(function (e) {
        e.preventDefault();
        if ($("#TXTSearchString").val() == undefined || $("#TXTSearchString").val() == '') {
            $('#alert_placeholder').html('<div class="alert alert-info"><a hreaf="#" class="close" data-dismiss="alert"><i class="icon-remove" style="margin-top:5px;"></i></a><strong>' + "Warning! " + '</strong><span> Please enter text to search </span></div>');
            loadPage(vm.currentPage(), vm.recordCount(), '');
        }
        else {
            vm.currentPage(1);
            var searchString = $("#TXTSearchString").val();
            loadPage(vm.currentPage(), vm.recordCount(), searchString);
        }
    });
});
var FirstPage = function (data) {
    if (!vm.isFirstPage()) {
        vm.currentPageIndex(0);
        vm.currentPage(vm.currentPageIndex(0))
        vm.currentPageIndex(vm.currentPageIndex(0));
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        vm.currentPage(1);
        loadPage(vm.currentPage(), vm.recordCount(), searchString);
    }
}
var LastPage = function (data) {
    if (!vm.isLastPage()) {
        vm.currentPage(vm.maxPageIndex() + 1)
        vm.currentPageIndex(vm.maxPageIndex() + 1);
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        vm.currentPage(vm.pager());
        loadPage(vm.pager(), vm.recordCount(), searchString);
    }
}
var previousPage = function (data) {
    if (!vm.isFirstPage()) {
        vm.currentPage(vm.currentPage() - 1);
        vm.currentPageIndex(vm.currentPageIndex() - 1);
        if (vm.isFirstPage()) {
            document.getElementById("prevPageButton").style.display = "initial";
        }
        document.getElementById("nextPageButton").style.display = "initial";
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        loadPage(vm.currentPage(), vm.recordCount(), searchString);
    }
    else {
    }
}

var nextPage = function (data) {
    if (!vm.isLastPage()) {
        var pageNumber = vm.currentPage() + 1;
        vm.currentPage(pageNumber);
        vm.currentPageIndex(vm.currentPageIndex() + 1);
        if (vm.isLastPage()) {
            document.getElementById("nextPageButton").style.display = "initial";
        }
        else {
            document.getElementById("nextPageButton").style.display = "initial";
        }
        document.getElementById("prevPageButton").style.display = "initial";
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        loadPage(vm.currentPage(), vm.recordCount(), searchString);
    }
    else {
        // loadPage(vm.pager(), vm.recordCount(), searchString);
    }
}

var pageSizeChanged = function (data) {
    if (vm.pageSize() != $("#rowsPerPage").val()) {
        vm.pageSize($("#rowsPerPage").val());
        vm.currentPage(1);
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        loadPage(vm.currentPage(), vm.recordCount(), searchString);
    }
}

var pagedRows = function (data) {
    if (vm.currentPage() != this.text) {
        vm.currentPage(Number(this.text));
        vm.currentPageIndex(Number(this.text) - 1);
        if (vm.isLastPage() && (vm.maxPageIndex() + 1) >= 5) {
            document.getElementById("nextPageButton").style.display = "initial";
            document.getElementById("prevPageButton").style.display = "initial";
        }
        else if (vm.isFirstPage() && (vm.maxPageIndex() + 1) >= 5) {
            document.getElementById("prevPageButton").style.display = "initial";
            document.getElementById("nextPageButton").style.display = "initial";
        }
        else if (!vm.isFirstPage() && !vm.isLastPage() && (vm.maxPageIndex() + 1) >= 5) {
            document.getElementById("prevPageButton").style.display = "initial";
            document.getElementById("nextPageButton").style.display = "initial";
        }
        var searchString = ($("#TXTSearchString").val() == undefined ? '' : $("#TXTSearchString").val());
        loadPage(vm.currentPage(), vm.recordCount(), searchString);
    }
}


